package com.example.planschedulingsystem.service;

import com.example.planschedulingsystem.entity.Plane;
import com.example.planschedulingsystem.repository.PlaneRepository;
import com.example.planschedulingsystem.repository.PlaneTripRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class PlaneManagerService {
    @Autowired
    private PlaneRepository planeRepository;

    @Autowired
    private PlaneTripRepository planeTripRepository;

    public Plane save(Plane plane) {
        return planeRepository.save(plane);
    }
    @Modifying
    @Transactional
    public Long delete(Plane plane) {
        Long plane_id = plane.getID();
        planeTripRepository.deletePlaneTripsByPlane_ID(plane_id);
        return planeRepository.deletePlaneByID(plane_id);
    }

    public List<Plane> findAll(){
        return (List<Plane>) planeRepository.findAll();
    }
}
